# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>
# Contributor:  Conrad Hoffmann <ch@bitfehler.net>

pkgname=netplan
pkgver=0.107
pkgrel=3
pkgdesc="Network configuration abstraction renderer"
arch=('loong64' 'x86_64')
url="https://github.com/CanonicalLtd/netplan"
license=('GPL3')
depends=(
  'dbus-python'
  'gcc-libs'
  'glibc'
  'glib2'
  'libyaml'
  'python'
  'python-cffi'
  'python-yaml'
  'python-netifaces'
  'python-rich'
  'iproute2'
  'systemd'
  'systemd-libs'
  'util-linux-libs'
)
makedepends=(
  'bash-completion'
  'cmocka'
  'meson'
#'pandoc'
  'python-coverage'
  'python-pycodestyle'
  'python-pyflakes'
  'python-pytest'
  'python-pytest-cov'
  'python'
  'pkgconf'
)
checkdepends=(
  'openvswitch'
  'wireless-regdb'
)
provides=(libnetplan.so)
source=(
  "$pkgname-$pkgver.tar.gz::$url/archive/${pkgver}.tar.gz"
  "$pkgname-0.107-disable_tests.patch"
  "$pkgname-0.107-use_lib.patch"
  netplan-disable-pandoc.patch
)
sha512sums=('6f6e3e4f179a2a74b46239cbd86919555a769d001b6fc8ba8daba3894b26415dbb8124f6a594de9afc14dc50d6979e58f195adfba2a53854b882d7cb6621fea7'
            '7f0b512a29c458e7e25f1387ce7f6730df47af48b35a5f2915a9f6a57fddf53a2c1d0690dfd81cd2dbbf40efa16f332bac63180ab3e589dcf5087f13775d040b'
            '64d6e97cb7c330fc2e28872a34e0be4190c6e75e5235de6ff97ef77c4c287f814b85503c798d959b88037fb53af0459c6a38768b7420f12aac79db64db127378')
b2sums=('8fcd3ae48e0f3e1eb0486cf4f7d76d17f61b3b8212363c9f00a1c1e6d265b1093cd7bc283e659091a44ef01c1346f47fc663d43387d72b95305ce2bb2c5b808a'
        'd8f5e36ef67fa43fa7c93eed2fbec54a4e934b9e994178820abda2473be646df009fc537ebc5fc067f8400c0ce3e28f66dfcf681b378cf2363f82acd79930149'
        '7ca1aa7b5449e23b310f32d16bf0f50525c4175d26eb591489143d0acef8cda7969502bff26402dbba10d3ea45c3c6768c28bf22c74b9f0a807bc953acf5871a')

prepare() {
  # adapt names of python things to our names
  # TODO: fix this upstream!
  sed -e 's/pytest3/pytest/;s/pyflakes3/pyflakes/;s/coverage-3/coverage/' -i $pkgname-$pkgver/meson.build
  # disable linting tests as they are broken and useless to us
  # disable unit-tests as we run them manually to be able to disable things that are broken
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-0.107-disable_tests.patch
  # use /usr/lib instead of /usr/libexec
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-0.107-use_lib.patch
  patch -p1 -i $srcdir/netplan-disable-pandoc.patch
}

build() {
  arch-meson $pkgname-$pkgver build
  meson compile -C build
}

check() {
  local pytest_options=(
    -vv
    # TODO: report/ fix upstream!
    --deselect $pkgname-$pkgver/tests/generator/test_args.py::TestConfigArgs::test_empty_config
    --deselect $pkgname-$pkgver/tests/generator/test_args.py::TestConfigArgs::test_no_configs
    --deselect $pkgname-$pkgver/tests/generator/test_args.py::TestConfigArgs::test_systemd_generator
    --deselect $pkgname-$pkgver/tests/generator/test_args.py::TestConfigArgs::test_systemd_generator_badcall
    --deselect $pkgname-$pkgver/tests/generator/test_args.py::TestConfigArgs::test_systemd_generator_noconf
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bond_auto_ovs_backend
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bond_lacp
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bond_mode_explicit_params
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bond_mode_implicit_params
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bond_setup
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bridge_auto_ovs_backend
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bridge_controller
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bridge_external_ids_other_config
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bridge_non_default_parameters
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bridge_non_ovs_bond
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bridge_set_protocols
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_bridge_setup
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_fake_vlan_bridge_setup
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_global_external_ids_other_config
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_global_ports
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_global_set_protocols
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_global_ssl
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_implicit_fake_vlan_bridge_setup
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_interface_external_ids_other_config
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_invalid_device_type
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_missing_ssl
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_no_ovs_config
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_ovs_duplicates_when_parser_needs_second_pass
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_ovs_invalid_networkd_config
    --deselect $pkgname-$pkgver/tests/generator/test_ovs.py::TestOpenVSwitch::test_patch_ports
    --deselect $pkgname-$pkgver/tests/generator/test_wifis.py::TestNetworkManager::test_wifi_regdom
  )

  meson test -C build

  # run unit tests separately so that we can disable broken tests
  export PYTHONPATH="build/python-cffi:$pkgname-$pkgver:$PYTHONPATH"
  export NETPLAN_GENERATE_PATH="build/src/generate"
  export NETPLAN_DBUS_CMD="build/dbus/netplan-dbus"
  LD_LIBRARY_PATH="build/src" pytest "${pytest_options[@]}"
}

package() {
  meson install -C build --destdir "$pkgdir"
  # remove legacy dir which will likely be gone post 0.107:
  # https://github.com/canonical/netplan/blob/a7e4be03918c986020650743cb6cf0934696ef0c/src/meson.build#L40-L44
  rm -frv "$pkgdir/lib"
}
